/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.model.smp;

import br.gov.cemaden.library.model.BaseEntity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonManagedReference;

/**
 *
 * @author paulo.dias
 */
@Entity
@Table(name = "smp_exec_aplic", schema = "smp")
@SequenceGenerator(name = "sequence", sequenceName = "smp.smp_exec_aplic_sequence", schema = "smp")
@NamedQueries({
    @NamedQuery(name = "ExecAplic.findAll", query = "SELECT a FROM ExecAplic a ORDER BY a.id ASC"),
    @NamedQuery(name = "ExecAplic.findById", query = "SELECT a FROM ExecAplic a WHERE a.id = :id"),
    @NamedQuery(name = "ExecAplic.findByPid", query = "SELECT a FROM ExecAplic a WHERE a.pid = :pid"),
    @NamedQuery(name = "ExecAplic.findByData", query = "SELECT a FROM ExecAplic a WHERE a.data < :data"),
    @NamedQuery(name = "ExecAplic.findByPidAndData", query = "SELECT a FROM ExecAplic a WHERE a.pid = :pid AND a.data = :data"),
    @NamedQuery(name = "ExecAplic.findByPidAplic", query = "SELECT a FROM ExecAplic a WHERE a.pidAplic = :pidAplic")
})
public class ExecAplic implements Serializable, Comparable<ExecAplic>, BaseEntity {

    @Id
    @GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
    @Column(name = "id_exec_aplic", nullable = false, unique = true)
    private Long id;

    @Column(name = "dataAtual")
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date data;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JsonBackReference
    @JoinColumn(name = "id_pid_aplic")
    private PIDAplic pidAplic;

    @Column(name = "pid", nullable = false)
    private Integer pid;

    @OneToOne(mappedBy = "execAplic", cascade = CascadeType.ALL)
    @JsonManagedReference
    private ThreadAplic thread;

    @OneToOne(mappedBy = "execAplic", cascade = CascadeType.ALL)
    @JsonManagedReference
    private MemoriaAplic memoria;

    @OneToOne(mappedBy = "execAplic", cascade = CascadeType.ALL)
    @JsonManagedReference
    private ClassesAplic classes;

    @OneToOne(mappedBy = "execAplic", cascade = CascadeType.ALL)
    @JsonManagedReference
    private CPUAplic cpu;

    public MemoriaAplic getMemoria() {
        return memoria;
    }

    public void setMemoria(MemoriaAplic memoria) {
        this.memoria = memoria;
    }

    public CPUAplic getCpu() {
        return cpu;
    }

    public void setCpu(CPUAplic cpu) {
        this.cpu = cpu;
    }

    public ClassesAplic getClasses() {
        return classes;
    }

    public void setClasses(ClassesAplic classes) {
        this.classes = classes;
    }

    public ThreadAplic getThread() {
        return thread;
    }

    public void setThread(ThreadAplic thread) {
        this.thread = thread;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getData() {
        return data;
    }

    public void setData(Date data) {
        this.data = data;
    }

   
    public PIDAplic getPidAplic() {
        return pidAplic;
    }

    public void setPidAplic(PIDAplic pidAplic) {
        this.pidAplic = pidAplic;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 17 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ExecAplic other = (ExecAplic) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ExecAplic{" + "id=" + id + ", data=" + data + ", pidAplic=" + pidAplic + ", pid=" + pid + '}';
    }

    public int compareTo(ExecAplic exc) {
        if (this.getData().before(exc.getData())) {
            return -1;
        } else if (this.getData().after(exc.getData())) {
            return 1;
        }
        if (getPid() == null) {
            if (exc.getPid() == null) {
                return 0;//sao iguais e null
            } else {
                return -1;
            }
        } else if (exc.getPid() == null) {
            return 1;
        }
        return getPid().compareTo(exc.getPid());
    }
}
